//
//  CYPayViewController.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 15/12/28.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYPayViewController.h"
#import "CYUser.h"
#import "CYHttpTool.h"
#import "WebViewJavascriptBridge.h"
#import "CYLoginHttpTool.h"
#import "MBProgressHUD+MJ.h"
#import "UIAlertView+Block.h"

@interface CYPayViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;

@end

@implementation CYPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    self.title = @"还款展示";
    CYUser *user = [CYLoginHttpTool userData].user;
    NSString *userid = user.userId;
    NSString *token = user.token;
    _myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _myWebView.backgroundColor = CYNaviThemeColorAndroid;
    _myWebView.delegate = self;
    //NSURL *httpUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.0.199:8080/app/loanList.htm?&userId=%@&token=%@",userid,token]];
     NSURL *httpUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.credit-manage.com:8058/app/loanList.htm?&userId=%@&token=%@",userid,token]];
    
//    NSURL *httpUrl = [NSURL URLWithString:@"https://www.baidu.com"];
    NSURLRequest *httpRequest=[NSURLRequest requestWithURL:httpUrl];
    [_myWebView loadRequest:httpRequest];
    //添加滑动返回
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [_myWebView addGestureRecognizer:rightSwipeGestureRecognizer];
   
    [self.view addSubview:_myWebView];
    [MBProgressHUD showMessage:nil toView:self.view];
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        [self.navigationController popViewControllerAnimated:YES];
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
    }
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
     [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
          if (buttonIndex == 0) {
               [[self navigationController] setNavigationBarHidden:NO animated:NO];
               [self.navigationController popViewControllerAnimated:YES];
               
          }
     }];
     
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //如果加载完成webview后，navi隐藏,加载失败，提供系统导航栏给用户返回
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    [MBProgressHUD hideHUDForView:self.view];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    [MBProgressHUD hideHUDForView:self.view];
    NSString *url = request.URL.absoluteString;
     
     if([url rangeOfString:@"report"].location !=NSNotFound)//_roaldSearchText
     {
          NSString *str = url;
          CYLog(@"str = %@",str);
          NSString *str1 = [str substringFromIndex:12];//截取掉下标11之后的字符串
          CYLog(@"str1 = %@",str1);
          if (str1 && !str1.isEmpty) {
               
               UIPasteboard *pab = [UIPasteboard generalPasteboard];
               [pab setString:str1];
               if (pab == nil) {
                    [MBProgressHUD showError:@"复制失败"];
                    
               }else
               {
                    [MBProgressHUD showSuccess:@"已复制"];
                    
               }
               
               
          }
     }
     else
     {
          NSLog(@"no");
     }
    //h5内控件字段方法:loanlist
    if([url hasSuffix:@"loanlist"]) {
        //返回上一级时，取消nav的隐藏
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
        //js交互oc获取到h5内的返回按钮后，点击返回，popvc
        [self.navigationController popViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
}
- (void)dealloc {
   
    [CYNotificationCenter removeObserver:self];
    [_myWebView didMoveToSuperview];
}

@end
